package nccu.model;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.compass.annotations.SearchableId;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.util.Date;

/**
 * This class represents the basic "submission" object that allows for xml file submission
 * persistence and management.
 *
 * @author <a href="mailto:chris.oberle@gmail.com">Chris Oberle</a>
 */
@Entity
@Table(name = "submission")
@XmlRootElement
public class SubmissionData extends BaseObject implements Serializable {

    private static final long serialVersionUID = 3832626162173359475L;

    private Long id;
    private Cycle cycle;
    private String originalFileName; // required
    private String fileName; // required but generated
    private String filePath; // requried but looked up from app config

    private CreditUnion creditUnion; // the credit union for whom this submission was for
    private Submission submission; // the header info for the submission

    /**
     * Default constructor - creates a new instance with no values set.
     */
    public SubmissionData(){}

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @SearchableId
    @Column (name="submission_id")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Embedded
    public Cycle getCycle() {
        return cycle;
    }

    public void setCycle(Cycle cycle){
        this.cycle = cycle;
    }

    @Column(name = "original_file_name", length = 200, nullable = false, unique = false)
    public String getOriginalFileName() {
        return originalFileName;
    }

    public void setOriginalFileName(String originalFileName) {
        this.originalFileName = originalFileName;
    }

    @Column(name = "file_name", length = 100, nullable = false, unique = false)
    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    @Column(name = "file_path", length = 100, nullable = false, unique = false)
    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    @Embedded
    public Submission getSubmission() {
        return submission;
    }

    public void setSubmission(Submission submission) {
        this.submission = submission;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof SubmissionData)) return false;

        SubmissionData that = (SubmissionData) o;

        if (!cycle.equals(that.cycle)) return false;
        if (!fileName.equals(that.fileName)) return false;
        if (!filePath.equals(that.filePath)) return false;
        if (!id.equals(that.id)) return false;
        if (!cycle.equals(that.cycle)) return false;

        return true;
    }

    @OneToOne
    @JoinColumn(name="creditunion_id")
    public CreditUnion getCreditUnion() {
        return creditUnion;
    }

    public void setCreditUnion(CreditUnion creditUnion) {
        this.creditUnion = creditUnion;
    }

    @Override
    public int hashCode() {
        int result = id.hashCode();
        result = 31 * result + cycle.hashCode();
        result = 31 * result + fileName.hashCode();
        result = 31 * result + filePath.hashCode();
        return result;
    }

    /**
     * {@inheritDoc}
     */
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE)
                .append(this.id)
                .append(this.cycle)
                .append(this.originalFileName)
                .append(this.fileName)
                .append(this.filePath)
                .toString();
    }
}
